package net.socket;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyStore;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

public class SecureOrderTaker
{
	public final static int DEFAULT_PROT = 7000;
	public final static String ALGORITHM = "SSL";
	
	public static void main(String[] args)
	{
		int port = DEFAULT_PROT;
		try
		{
			SSLContext context = SSLContext.getInstance(ALGORITHM);
			KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
			KeyStore ks = KeyStore.getInstance("JSK");
			char [] password = "newair".toCharArray();
			ks.load(new FileInputStream("jnp3e.keys"),password);
			kmf.init(ks,password);
			context.init(kmf.getKeyManagers(), null, null);
			SSLServerSocketFactory factory = context.getServerSocketFactory();
			SSLServerSocket server = (SSLServerSocket) factory.createServerSocket(port);
			String [] supported = server.getEnabledCipherSuites();
			String [] anonChiperSuitesSupported = new String[supported.length];
			int numAnonCipherSuitesSupported = 0;
			for (int i = 0; i < supported.length; i++)
			{
				if(supported[i].indexOf("_anno_")>0)
				{
					anonChiperSuitesSupported[numAnonCipherSuitesSupported++] = supported[i];
				}
			}
			String [] oldEnabled = server.getEnabledCipherSuites();
			String [] newEnabled = new String[oldEnabled.length+numAnonCipherSuitesSupported];
			System.arraycopy(oldEnabled, 0, newEnabled, 0, oldEnabled.length);
			System.arraycopy(anonChiperSuitesSupported, 0, newEnabled,oldEnabled.length,numAnonCipherSuitesSupported);
			server.setEnabledCipherSuites(newEnabled);
			try
			{
				while(true)
				{
					Socket theConnection = server.accept();
					InputStream in = theConnection.getInputStream();
					int c =0;
					while((c=in.read())!=-1)
					{
						System.out.write(c);
					}
					theConnection.close();
				}
			}
			catch (Exception e)
			{
			}
		}
		catch (Exception e)
		{
		}
	}
}
